package dao

import (
	__ "base_srv/basic/baseproto"
	"base_srv/handler/models"
	"errors"
	"fmt"
	"time"
)

// ApplicationList 应用列表
func ApplicationList() ([]*__.ApplicationList, error) {

	var a models.Application
	list, err := a.List()
	if err != nil {
		return nil, errors.New("查询失败:" + err.Error())
	}
	var respList []*__.ApplicationList
	for _, v := range list {
		l := __.ApplicationList{
			Id:              int64(v.Id),
			AppName:         v.AppName,
			AppSortOrder:    int32(v.AppSortOrder),
			AppIcon:         v.AppIcon,
			AppPackageName:  v.AppPackageName,
			AppStatus:       int32(v.AppStatus),
			UserName:        v.RealName,
			UploadTime:      v.UploadTime.Format("2006-01-02 15:04:05"),
			StartUpdateTime: v.StartUpdateTime.Format("2006-01-02 15:04:05"),
		}
		respList = append(respList, &l)
	}
	return respList, nil
}

// ApplicationAdd 应用添加
func ApplicationAdd(in *__.ApplicationAddReq) (uint32, error) {
	t, err := time.Parse(time.DateTime, in.StartUpdateTime)
	if err != nil {
		return 0, errors.New("时间格式错误")
	}
	a := models.Application{
		AppName:         in.AppName,
		AppSortOrder:    uint32(in.AppSortOrder),
		AppIcon:         in.AppIcon,
		AppPackageName:  in.AppPackageName,
		AppStatus:       int8(in.AppStatus),
		UserId:          uint32(in.UserId),
		StartUpdateTime: t.Add(-time.Hour * 8),
		UploadTime:      time.Now(),
	}
	if err = a.Create(); err != nil {
		return 0, errors.New("添加失败:" + err.Error())
	}
	return a.Id, nil
}

// 应用更新
func ApplicationUpdate(in *__.ApplicationUpdateReq) error {
	a := models.Application{
		Id:             uint32(in.Id),
		AppName:        in.AppName,
		AppSortOrder:   uint32(in.AppSortOrder),
		AppIcon:        in.AppIcon,
		AppPackageName: in.AppPackageName,
		AppStatus:      int8(in.AppStatus),
		UploadTime:     time.Now(),
	}
	var t time.Time
	var err error
	if in.StartUpdateTime != "" {
		fmt.Println(in.StartUpdateTime)

		t, err = time.Parse(time.DateTime, in.StartUpdateTime)
		if err != nil {
			return errors.New("时间格式错误")
		}
		fmt.Println(t)
		a.StartUpdateTime = t.Add(-time.Hour * 8)
	}
	if err = a.Update(); err != nil {
		return errors.New("更新失败:" + err.Error())
	}
	return nil
}

// ApplicationUpdateWithoutFile 应用修改（无文件）
func ApplicationUpdateWithoutFile(in *__.ApplicationUpdateWithoutFileReq) error {
	a := models.Application{
		Id:             uint32(in.Id),
		AppName:        in.AppName,
		AppSortOrder:   uint32(in.AppSortOrder),
		AppIcon:        in.AppIcon,
		AppPackageName: in.AppPackageName,
		AppStatus:      int8(in.AppStatus),
	}
	var t time.Time
	var err error
	if in.StartUpdateTime != "" {
		fmt.Println(in.StartUpdateTime)

		t, err = time.Parse(time.DateTime, in.StartUpdateTime)
		if err != nil {
			return errors.New("时间格式错误")
		}
		fmt.Println(t)
		a.StartUpdateTime = t.Add(-time.Hour * 8)
	}
	if err = a.Update(); err != nil {
		return errors.New("更新失败:" + err.Error())
	}
	return nil
}

// 应用删除
func ApplicationDelete(in *__.ApplicationDeleteReq) error {
	var a models.Application
	a.Id = uint32(in.Id)
	if err := a.Delete(); err != nil {
		return errors.New("删除失败:" + err.Error())
	}
	return nil
}
